/**
 * 
 */
package com.novartis.nims.imagedc.application;

import java.beans.PropertyChangeEvent;
import java.io.File;

import javax.swing.SwingWorker;

import org.apache.log4j.Logger;

import com.novartis.nims.imagedc.logic.ScanManager;
import com.novartis.nims.imagedc.logic.WorkflowController;

/**
 * @author Josh Snyder
 *
 */
class ApplicationController implements WorkflowController {
	
	private static Logger logger = Logger.getLogger(ApplicationController.class);

	private ApplicationView view;
	private ScanManager scanManager;
	private SwingWorker<Void, Void> workerThread;
	
	ApplicationController(ApplicationView theView, ScanManager theScanManager) {
		view = theView;
		scanManager = theScanManager;
		
		workerThread = new SwingWorker<Void, Void>() {
			protected Void doInBackground() {
				return null; //default does nothing
			}
		};
	}

	public boolean isCancelled() {
		return workerThread.isCancelled();
	}

	public void propertyChange(PropertyChangeEvent event) {
		workerThread.getPropertyChangeSupport().firePropertyChange(event);
	}
	
	void loadAndIdentifyScans(File directory, String studyId) {
		logger.debug("loadAndIdentifyScans called");
		workerThread = new LoadAndIdentifySwingWorker(directory, studyId);
		view.registerWorker(workerThread);
		workerThread.execute();
	}
	
	private class LoadAndIdentifySwingWorker extends SwingWorker<Void, Void> {

		private File directory;
		private String studyId;
		
		LoadAndIdentifySwingWorker(File theDirectory, String theStudyId) {
			directory = theDirectory;
			studyId = theStudyId;
		}
		
		@Override
		protected Void doInBackground() throws Exception {
			logger.debug("doInBackground called");
			scanManager.loadScansFromDirectory(directory);
			scanManager.calculateScanIdentifiers();
			return null;
		}
		
		@Override
		protected void done() {
			setProgress(100);
			try {
				get();
				scanManager.setScanStudyIds(studyId);
				view.updateTableModel(new ScanTableModel(scanManager.getScans(), scanManager.getScanSelections()));
//				view.getJButtonSubmitScans().setEnabled(true);
				view.next();
				view.maximize();
			} catch (Exception e) {
				scanManager.clearScans();
//				view.getJButtonSubmitScans().setEnabled(false);
			}
		}
	}
	
	void updateAndSubmitScans(boolean anonymizeFlag) {
		logger.debug("confirmAndSubmitScans() called");
		workerThread = new UpdateAndSubmitSwingWorker(anonymizeFlag);
		view.registerWorker(workerThread);
		workerThread.execute();
	}
	
	private class UpdateAndSubmitSwingWorker extends SwingWorker<Void, Void> {

		private boolean anonymizeFlag;
		
		UpdateAndSubmitSwingWorker(boolean theAnonymizeFlag) {
			anonymizeFlag = theAnonymizeFlag;
		}
		
		@Override
		protected Void doInBackground() throws Exception {
			logger.debug("doInBackground called");
			scanManager.updateAndSubmitScans(anonymizeFlag);
			return null;
		}
		
		@Override
		protected void done() {
			setProgress(100);
			try {
				get();
				view.next();
				view.clearTable();
				view.pack();
			} catch (Exception e) {
//				view.getJButtonSubmitScans().setEnabled(false);
			}
		}
	}
	
	void clearScans() {
		scanManager.clearScans();
	}

}
